package com.frxuexi.park2209.park

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.ColorBean
import com.frxuexi.base.entity.ParkListItem
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.base.net.Const
import com.frxuexi.park2209.R
import com.frxuexi.park2209.adapter.CarImageAdapter
import com.frxuexi.park2209.adapter.ColorAdapter
import com.frxuexi.park2209.databinding.ActivityParkAddBinding
import com.google.gson.Gson
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding,ParkViewModel> (){
    lateinit var colorAdapter: ColorAdapter
    lateinit var carImageAdapter: CarImageAdapter
    override fun initData() {
        lifecycleScope.launch {
            vm.colorSuccess.collect{
                if(it.isNotEmpty()) {
                    val colorBean = Gson().fromJson(it, ColorBean::class.java)
                    colorAdapter.addData(colorBean)
                    colorAdapter.notifyDataSetChanged()
                }
            }
        }

        lifecycleScope.launch {
            vm.addParkSucRoom.collect{
                if(it>0) {
                    ToastUtils.showLong("申请成功")
                    finish()
                }
            }
        }

        lifecycleScope.launch {
            vm.addParkSuccess.collect{
                if(it.isNotEmpty()) {
                    ToastUtils.showLong("申请成功")
                    finish()
                }
            }
        }
    }

    override fun initView() {
        val uname = SPUtils.getInstance().getString(Const.UNAME, "")
        binding.parkAuthorName.text=uname
        colorAdapter= ColorAdapter()
        binding.rvColor.apply {
            adapter=colorAdapter
            layoutManager=LinearLayoutManager(this@ParkAddActivity,RecyclerView.HORIZONTAL,false)
        }

        colorAdapter.setOnItemClickListener { adapter, view, position ->
            val colorValue = colorAdapter.data.get(position).colorValue
            binding.parkSelColor.setBackgroundColor(Color.parseColor(colorValue))
        }
        vm.getColors()

        binding.headParkadd.apply {
            headerTitle.text="车位申请"
            ivBack.setOnClickListener {
                finish()
            }
        }

        binding.parkCarAdd.setOnClickListener {
            PictureSelector.create(this@ParkAddActivity,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        carImageAdapter= CarImageAdapter()
        binding.rvCarimg.apply {
            adapter=carImageAdapter
            layoutManager=GridLayoutManager(this@ParkAddActivity,4)
        }

        binding.parkIvstart.setOnClickListener {
            showDateDialog(binding.parkStartTime)
        }

        binding.parkIvend.setOnClickListener {
            showDateDialog(binding.parkEndTime)
        }

        binding.parkStartTime.text=getDate(System.currentTimeMillis())
        binding.parkEndTime.text=getDate(System.currentTimeMillis())


        binding.btnParkcommit.setOnClickListener {
            val map = mutableMapOf<String, Any>()
//            map["parkUser"]=SPUtils.getInstance().getInt(Const.UID, -1)
//            map["parkName"]=uname
//            map["parkStart"]=binding.parkStartTime.text
//            map["parkEnd"]=binding.parkEndTime.text
//            map["parkTime"]=getDataTime(System.currentTimeMillis())
//            vm.addPark(map)

            vm.addPark(ParkListItem(binding.parkEndTime.text.toString(),
                null,uname,binding.parkStartTime.text.toString(),
                getDataTime(System.currentTimeMillis()),0))
        }

    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            carImageAdapter.apply {
                addData(bean?.path!!)
                notifyDataSetChanged()
            }

        }
    }

}